package com.gosling.cloudspace.listener;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Slf4j
//@Component
//@RocketMQMessageListener(
//        topic = MQTopicConstant.TOPIC_NORMAL,
//        consumerGroup = MQTopicConstant.TAG_GID_FINANCE_SETTLE,
//        selectorExpression = MQTopicConstant.TAG_FINANCE_SETTLE
//)
@Transactional
//public class FinanceSettleMqListener extends RocketEnhanceListener<OrderInfoDto> {
public class FinanceSettleMqListener<OrderInfoDto> {

    private Map<String, Function<OrderInfoDto, Boolean>> orderSettleMap;

    @PostConstruct
    public void initSettleMethod() {
        this.orderSettleMap = new HashMap<>();
        orderSettleMap.put("channel_credit", this::channelCreditSettle);
        orderSettleMap.put("product_credit", this::productCreditSettle);
        orderSettleMap.put("product_loan", this::productLoanSettle);
        orderSettleMap.put("product_repay", this::productRepaySettle);
    }

    //    @Override
    public void consumerMessage(OrderInfoDto message) {
        log.info("FinanceSettleMqListener 收到消息:{}", JSONUtil.toJsonStr(message));
        orderSettleMap.get("channel_credit").apply(message);
        log.info("FinanceSettleMqListener 处理完消息:{}", JSONUtil.toJsonStr(message));
    }

//    public void onError(Throwable exception, FinanceSettleMq message, int currentRetryNum, int maxRetryNum) {
//        if (currentRetryNum >= maxRetryNum) {
//            log.error("FinanceSettleMqListener 消费消息异常:{}", JSONUtil.toJsonStr(message), exception);
//            //最后一次消费失败才发送告警消息，避免重复告警
//            ExpBO repayReq = new ExpBO();
//            repayReq.setMsg(String.format("财务流水执行异常:%s", exception.getMessage()));
//            repayReq.setTitleEnum(PrometheusTitleEnum.OTHER);
//            repayReq.setReqData(JSONUtil.toJsonStr(message));
//            PrometheusUtil.insert(repayReq);
//        }
//    }

    private boolean channelCreditSettle(OrderInfoDto message) {
        return true;
    }

    private boolean productCreditSettle(OrderInfoDto message) {
        return true;
    }

    private boolean productLoanSettle(OrderInfoDto message) {
        return true;
    }

    private boolean productRepaySettle(OrderInfoDto message) {
        return true;
    }

}
